﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Galaktika.BI.Silverlight.Controls.PivotGrid.MemberActions
{
    public class HistoryManager
    {
        private IList<HistoryItem> m_History = new List<HistoryItem>();

        public void ClearHistory()
        {
            m_History.Clear();
            m_CurrentHistoryItem = null;
        }

        /// <summary>
        /// Удаляет все элементы истории, стоящие за текущим
        /// </summary>
        public void CutRight()
        {
            if (CurrentHistoryItem != null)
            {
                int indx = m_History.IndexOf(CurrentHistoryItem);
                if(indx > -1)
                {
                    while(m_History.Count > indx + 1)
                    {
                        m_History.RemoveAt(indx + 1);
                    }
                }
            }
        }

        public void AddHistoryItem(HistoryItem item)
        {
            if (item != null)
            {
                m_History.Add(item);
                m_CurrentHistoryItem = item;
            }
        }

        public int Size
        {
            get {
                return m_History.Count;
            }
        }

        public int CurrentHistiryItemIndex
        {
            get
            {
                if (CurrentHistoryItem != null)
                {
                    int indx = m_History.IndexOf(CurrentHistoryItem);
                    if (indx >= 0)
                        return indx;
                }
                return -1;
            }
        }

        /*public bool TestToFirst(HistoryItem item)
        {
            if (item == null)
                return false;

            if (m_History.Count > 0 && m_History.IndexOf(item) == 0)
                return true;
            else
                return false;
        }

        public bool TestToLast(HistoryItem item)
        {
            if (item == null)
                return false;

            if (m_History.Count > 0 && m_History.IndexOf(item) == (m_History.Count - 1))
                return true;
            else
                return false;
        }*/

        HistoryItem m_CurrentHistoryItem;
        /// <summary>
        /// Текущий элемент истории
        /// </summary>
        public HistoryItem CurrentHistoryItem
        {
            get
            {
                return m_CurrentHistoryItem;
            }
        }

        public void MoveBack()
        {
            if (CurrentHistoryItem != null && m_History.Count > 0)
            {
                int indx = m_History.IndexOf(CurrentHistoryItem);
                if (indx > 0)
                {
                    m_CurrentHistoryItem = m_History[indx - 1];
                }
            }
        }

        public void MoveNext()
        {
            if (CurrentHistoryItem != null && m_History.Count > 0)
            {
                int indx = m_History.IndexOf(CurrentHistoryItem);
                if (indx >= 0 && indx < m_History.Count - 1)
                {
                    m_CurrentHistoryItem = m_History[indx + 1];
                }
            }
        }

        public void ToBegin()
        {
            if (m_History.Count > 0)
            {
                m_CurrentHistoryItem = m_History[0];
            }
        }

        public void ToEnd()
        {
            if (m_History.Count > 0)
            {
                m_CurrentHistoryItem = m_History[m_History.Count - 1];
            }
        }
    }
}
